package com.zzyl.nursing.service.impl;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingPlanStatusDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingProjectPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @Author: Zhy
 * @Date: 2025-02-21
 */
@Service
@RequiredArgsConstructor
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {

	private final NursingPlanMapper nursingPlanMapper;
	private final INursingProjectPlanService nursingProjectPlanService;
	private final NursingProjectPlanMapper nursingProjectPlanMapper;

	/**
	 * 查询护理计划
	 *
	 * @param: id 护理计划主键
	 * @return: 护理计划
	 */
	@Override
	public NursingPlanVo selectNursingPlanById(Integer id) {
		// 查询计划
		NursingPlan plan = getById(id);

		// 查询计划项目
		List<NursingProjectPlan> projectPlans = nursingProjectPlanMapper.selectList(new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId,
			id
		));

		// 组装返回结果
		NursingPlanVo nursingPlanVo = new NursingPlanVo();
		BeanUtils.copyProperties(plan, nursingPlanVo);
		nursingPlanVo.setProjectPlans(projectPlans);

		return nursingPlanVo;
	}

	/**
	 * 查询护理计划列表
	 *
	 * @param: nursingPlan 护理计划
	 * @return: 护理计划
	 */
	@Override
	public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
		return nursingPlanMapper.selectNursingPlanList(nursingPlan);
	}

	/**
	 * 新增护理计划
	 *
	 * @param: nursingPlan 护理计划
	 * @return: 结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
		// 新增护理计划
		NursingPlan nursingPlan = new NursingPlan();
		BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
		boolean flag = save(nursingPlan);
		if (!flag) {
			throw new BaseException("新增计划失败");
		}

		// 新增护理项目
		List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
		projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
		flag = nursingProjectPlanService.saveBatch(projectPlans);
		if (!flag) {
			throw new BaseException("新增护理项目失败");
		}

		return 1;
	}

	/**
	 * 修改护理计划
	 *
	 * @param: nursingPlan 护理计划
	 * @return: 结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
		// 修改计划
		NursingPlan nursingPlan = new NursingPlan();
		BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
		boolean flag = updateById(nursingPlan);
		if (!flag) {
			throw new BaseException("修改计划失败");
		}

		// 删除原有项目
		flag = nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId,
			nursingPlan.getId()
		));
		if (!flag) {
			throw new BaseException("删除原有项目失败");
		}

		if (ObjectUtil.isEmpty(nursingPlanDto.getProjectPlans())) {
			return 1;
		}

		List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
		projectPlans.forEach(v -> v.setPlanId(nursingPlan.getId()));
		flag = nursingProjectPlanService.saveBatch(projectPlans);
		if (!flag) {
			throw new BaseException("新增护理项目失败");
		}

		return 1;
	}

	/**
	 * 修改护理计划状态
	 *
	 * @return:
	 * @param:
	 */
	@Override
	public int updateNursingStatusPlan(NursingPlanStatusDto nursingPlanStatusDto) {
		// 检查传入的DTO对象是否为空
		if (ObjectUtil.isEmpty(nursingPlanStatusDto)) {
			throw new BaseException("传入的护理计划状态信息为空");
		}

		Integer planId = nursingPlanStatusDto.getId();
		Integer newStatus = nursingPlanStatusDto.getStatus();

		// 更新护理计划的状态
		NursingPlan nursingPlan = new NursingPlan();
		nursingPlan.setId(planId);
		nursingPlan.setStatus(newStatus);
		boolean flag = updateById(nursingPlan);

		if (!flag) {
			throw new BaseException("修改护理计划状态失败");
		}

		return 1;
	}

	/**
	 * 批量删除护理计划
	 *
	 * @param: ids 需要删除的护理计划主键
	 * @return: 结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteNursingPlanByIds(Integer[] ids) {
		return removeBatchByIds(Arrays.asList(ids)) ? 1 : 0;
	}

	/**
	 * 删除护理计划信息
	 *
	 * @param: id 护理计划主键
	 * @return: 结果
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteNursingPlanById(Integer id) {
		// 删除项目
		nursingProjectPlanService.remove(new LambdaQueryWrapper<NursingProjectPlan>().eq(NursingProjectPlan::getPlanId,
			id
		));

		// 删除计划
		removeById(id);

		return 1;
	}

	/**
	 * 查询所有护理计划
	 *
	 * @return:
	 * @param:
	 */
	@Override
	public List<NursingProjectVo> getPlanAll() {
		return nursingPlanMapper.getIsEnablePlan();
	}

}
